import {createStore,combineReducers,applyMiddleware,compose} from 'redux';
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'remote-redux-devtools';

import official from './reducers/official';
const composeEnhancers = composeWithDevTools({ realtime: true, port: 3000 });
// import car from './reducers/car';

/**
 * {
 *      car,
 *      official,
 *      ....
 * }
 * 
*/

function getReducer(){
    let files = require.context('./reducers/',true,/\.js$/);
    console.log(files.keys())
    return files.keys().reduce((prev,cur) => {
        let key = cur.match(/\.\/(\w+)\.js$/)[1];  ///car  official
        prev[key] = files(cur).default;
        return prev
    },{})
}

console.log(getReducer())



//默认的state
// const defaultState = {
//     official:{
//         brandList:[]
//     },
//     img:{

//     },
//     car:{

//     },
//     quotation:{

//     }
// }

//combineReducers的实现原理
//{car:()=>{},official:()=>{}}
// const combineReducers = (options) => {
//     return (state,action) => {
//         //['car','official',...]
//         return Object.keys(options).reduce((prev,cur) => {
//             prev[cur] = options[cur](state ? state[cur] : state,action);
//             return prev
//         },{})
//     }
// }

let reducers = combineReducers(getReducer());

const store = createStore(reducers,composeEnhancers(applyMiddleware(thunk))); //reducer是一个函数

/**
 * createStore((state,action) => {
 *      return state
 * })
 * }
 * 
 * 
*/
window.store = store;

export default store